其他
几行代码构建全功能的对象检测模型,他是如何做到的?
然后,将上面的图像另存为“fruit.jpg”,并在与图像相同的文件夹中创建一个Python文件。在Python文件中,编写以下5行代码:
from
detecto
import
core, utils, visualize
image = utils.read_image(
'fruit.jpg'
)
model = core.Model()
labels, boxes, scores = model.predict_top(image)
visualize.show_labeled_image(image, boxes, labels)
运行此文件后(如果你的计算机上没有启用CUDA的GPU,可能会花费几秒钟;稍后再进行介绍),你应该会看到类似下面的图:
from
detecto.utils
import
split_video
split_video(
'video.mp4'
,
'frames/'
, step_size=
4
)
images/
| image0.jpg
| image1.jpg
| image2.jpg
| ...
pip3 install labelImg
# Download LabelImg using pip
labelImg
# Launch the application
images/
| image0.jpg
| image0.xml
| image1.jpg
| image1.xml
| ...
import
torch
print(torch.cuda.is_available())
import
os
from
google.colab
import
drive
drive.mount(
'/content/drive'
)
os.chdir(
'/content/drive/My Drive/Detecto Tutorial'
)
!pip install detecto
from
detecto
import
core, utils, visualize
dataset = core.Dataset(
'images/'
)
model = core.Model([
'alien'
,
'bat'
,
'witch'
])
model.fit(dataset)
# Specify the path to your image
image = utils.read_image(
'images/image0.jpg'
)
predictions = model.predict(image)
# predictions format: (labels, boxes, scores)
labels, boxes, scores = predictions
# ['alien', 'bat', 'bat']
print(labels)
# xmin ymin xmax ymax
# tensor([[ 569.2125, 203.6702, 1003.4383, 658.1044],
# [ 276.2478, 144.0074, 579.6044, 508.7444],
# [ 277.2929, 162.6719, 627.9399, 511.9841]])
print(boxes)
# tensor([0.9952, 0.9837, 0.5153])
print(scores)
visualize.show_labeled_image(image, boxes, labels)
visualize.detect_video(model,
'input.mp4'
,
'output.avi'
)
model.save(
'model_weights.pth'
)
# ... Later ...
model = core.Model.load(
'model_weights.pth'
, [
'alien'
,
'bat'
,
'witch'
])
from
torchvision
import
transforms
augmentations = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomHorizontalFlip(
0.5
),
transforms.ColorJitter(saturation=
0.5
),
transforms.ToTensor(),
utils.normalize_transform(),
])
dataset = core.Dataset(
'images/'
, transform=augmentations)
loader = core.DataLoader(dataset, batch_size=
2
, shuffle=
True
)
import
matplotlib.pyplot
as
plt
val_dataset = core.Dataset(
'validation_images/'
)
losses = model.fit(loader, val_dataset, epochs=
10
, learning_rate=
0.001
,
lr_step_size=
5
, verbose=
True
)
plt.plot(losses)
plt.show()
原文:
https://hackernoon.com/build-a-custom-trained-object-detection-model-with-5-lines-of-code-y08n33vi
(*本文由AI科技大本营翻译,转载请微信联系1092722531)
◆
精彩推荐
◆
技术战“疫”,贾扬清、李飞飞要给程序员直播讲AI技术!
2月18日、2月20日晚7点,阿里云CIO学院攻“疫”技术课程正式开启。您将获得与达摩院数据库首席科学家 、阿里巴巴集团副总裁、ACM 杰出科学家李飞飞,Caffe之父、ONNX创始人、阿里巴巴集团副总裁贾扬清,阿里巴巴集团副总裁、阿里 CIO 学院院长胡臣杰等顶级技术专家直播互动的机会。
你点的每个“在看”,我都认真当成了AI